package com.cn.config;


import com.cn.controller.interceptor.ProjectInterceptor;
import com.cn.controller.interceptor.ProjectInterceptor2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@ComponentScan({"com.cn.controller"})
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {
    @Autowired
    private ProjectInterceptor projectInterceptor;
    @Autowired
    private ProjectInterceptor2 projectInterceptor2;

    //在SpringMvc配置拦截器，简化开发
    public void addInterceptors(InterceptorRegistry registry) {
        //配置拦截器
        //这个是指会拦截/books请求或者/books/*请求
        registry.addInterceptor(projectInterceptor).addPathPatterns("/books","/books/*");
        registry.addInterceptor(projectInterceptor2).addPathPatterns("/books","/books/*");
    }

    //配置过滤器，让css,js等资源能够被访问到
    //用户通过浏览器请求 http://localhost:8080/pages/index.html。
    //Spring 根据 registry.addResourceHandler("/pages/**") 匹配到请求。
    //然后，Spring 去 addResourceLocations("/pages/") 指定的路径中寻找文件（例如：src/main/resources/pages/index.html）。
    //找到后，将文件返回给客户端。
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/pages/**").addResourceLocations("/pages/");
    }
}
